package com.be.beadmin.system.impl.ProductMaterialServiceImpl;

import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.product.dto.output.ProductManufactExecInfoEx;
import com.be.beadmin.product.entity.ProductManufactInfor;
import com.be.beadmin.product.entity.WorkOrderInfor;
import com.be.beadmin.product.mapper.ProductManufactInforMapper;
import com.be.beadmin.product.mapper.ProductManufactInforMapperEx;
import com.be.beadmin.product.mapper.WrokingHoursDetailsMapper;
import com.be.beadmin.product.repository.ProductManufactInforRepository;
import com.be.beadmin.product.repository.WorkOrderInforRepository;
import com.be.beadmin.product.repository.WrokingHoursDetailsRepository;
import com.be.beadmin.product.repository.WrokingHoursOverViewRepository;
import com.be.beadmin.product.service.PlantCapacityCalendarService;
import com.be.beadmin.product.service.WorkOrderInforService;
import com.be.beadmin.project.entity.BatchProduct;
import com.be.beadmin.project.repository.BatchPlanRepository;
import com.be.beadmin.project.repository.BatchProductRepository;
import com.be.beadmin.project.repository.PlanStageRepository;
import com.be.beadmin.project.service.BatchPlanService;
import com.be.beadmin.service.repository.FactoryCalendarRepository;
import com.be.beadmin.system.service.ProductMaterialService.ProductManufactInforPmService;
import com.be.beadmin.system.service.ProductMaterialService.WorkOrderInforPmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @author: Jiaming Zheng
 * @Date: Create in 17:42 2023/6/24
 * @Modified By:
 **/
@Service
public class ProductManufactInforPmServiceImpl implements ProductManufactInforPmService {

    @Autowired
    private ProductManufactInforRepository productManufactInforRepository;


    @Autowired
    private ProductManufactInforMapperEx productManufactInforMapperEx;
    @Autowired
    private BatchProductRepository batchProductRepository;


    @Autowired
    private WorkOrderInforService workOrderInforService;
    @Autowired
    private WorkOrderInforPmService workOrderInforPmService;
    @Autowired
    private WorkOrderInforRepository workOrderInforRepository;




    /**
     * 点击工序执行的工序需要获取到的信息
     *
     * @param productManufactInforId
     * @return
     */
    @Override
    public ProductManufactExecInfoEx getProductManufactExecInfo(String productManufactInforId) {

        ProductManufactInfor productManufactInfor = productManufactInforRepository.findProductManufactInforByIdAndDelete(productManufactInforId, 0);
        if (productManufactInfor == null) throw new BeadminException("未找到该工序");
        BatchProduct product = batchProductRepository.findBatchProductByIdAndDelete(productManufactInfor.getBatchProductId(), 0);
        if (product == null)
            throw new BeadminException("未找到该柜体：[" + productManufactInfor.getBatchProductId() + "]");
        WorkOrderInfor workOrderInfor = workOrderInforRepository.findWorkOrderInforByBatchProductIdAndDelete(product.getId(), 0);
        if (workOrderInfor == null)
            throw new BeadminException("未找到该柜体工单，柜体code为：[" + product.getProductCode() + "]");

        ProductManufactExecInfoEx productManufactExecInfoEx = new ProductManufactExecInfoEx();
        productManufactExecInfoEx.setCurrentProductManufactInfor(productManufactInfor);
        productManufactExecInfoEx.setWorkOrderInfor(workOrderInfor);
        productManufactExecInfoEx.setBomOverviews(workOrderInforPmService.getBomOverview(workOrderInfor.getId()));
        productManufactExecInfoEx.setDrawingInfos(workOrderInforService.getDrawingInfo(workOrderInfor.getId()));
        productManufactExecInfoEx.setAllManufactInfors(workOrderInforService.getManufactInfo(workOrderInfor.getId()));

        return productManufactExecInfoEx;
    }
}
